﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Joyintech.Common;
using Joyintech.Model.inpiao365;
using Joyintech.BLL.inpiao365;

namespace Joyintech.Admin.inpiao365.general.system.users
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //绑定部门（如果是修改部门则需排除其自身及其下级部门）
                DataBindsystemDepartment();

                //绑定用户列表
                DataBindSystemUser();
            }
        }

        #region 绑定部门（如果是修改部门则需排除其自身及其下级部门）

        /// <summary>
        /// 绑定所有部门
        /// </summary>
        private void DataBindsystemDepartment()
        {
            this.ddlDeptPID.Items.Clear();

            this.ddlDeptPID.Items.Add(new ListItem("-所有部门-", "0"));

            List<systemDepartment> list = new List<systemDepartment>();

            list = new systemDepartment_BLL().GetModelList("deptPID=0");

            foreach (systemDepartment model in list)
            {
                this.ddlDeptPID.Items.Add(new ListItem(model.DeptName, model.DeptID.ToString()));

                //递归绑定下级部门
                DataBindsystemDepartmentChild(model.DeptID, "--");
            }
        }

        /// <summary>
        /// 递归绑定下级部门
        /// </summary>
        /// <param name="menuPID"></param>
        private void DataBindsystemDepartmentChild(int deptPID, string frontStr)
        {
            List<systemDepartment> list = new List<systemDepartment>();

            list = new systemDepartment_BLL().GetModelList("deptPID=0" + deptPID);

            foreach (systemDepartment model in list)
            {
                this.ddlDeptPID.Items.Add(new ListItem(frontStr + model.DeptName, model.DeptID.ToString()));

                //递归绑定下级部门
                DataBindsystemDepartmentChild(model.DeptID, "--" + frontStr);
            }
        }

        #endregion

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            DataBindSystemUser();
        }

        private void DataBindSystemUser()
        {
            StringBuilder strSql = new StringBuilder();

            if (this.txtUserName.Text.Trim() != "")
            {
                strSql.Append(" and a.UserName like '%" + this.txtUserName.Text.Trim() + "%'");
            }

            if (this.ddlDeptPID.SelectedValue != "0")
            {
                strSql.Append(" and a.DeptId=" + this.ddlDeptPID.SelectedValue);
            }

            this.SqlPager1.CurrentPageIndex = 0;
            this.SqlPager1.ConnStringKeyName = "localServer";
            this.SqlPager1.SelectCommand = "a.*,b.deptName";
            this.SqlPager1.SelectCommandTable = "systemUser a,systemDepartment b";
            this.SqlPager1.SelectCommandWhere = "a.deptId=b.deptId and UserState<>2" + strSql.ToString();
            this.SqlPager1.SortField = "UserId";
            this.SqlPager1.SortStyle = WebControls.SortMode.DESC;
            this.SqlPager1.DataBind();
        }

        /// <summary>
        /// 获取用户状态
        /// </summary>
        /// <param name="UserState"></param>
        /// <returns></returns>
        protected string GetUserState(string UserState)
        {
            string retState = "正常";

            switch (UserState)
            { 
                case "0":
                    retState = "正常";
                    break;
                case "1":
                    retState = "禁用";
                    break;
                case "2":
                    retState = "删除";
                    break;
            }

            return retState;
        }
    }
}